package com.cby.operation.common.ui

import android.net.http.SslError
import android.view.View
import android.webkit.*
import com.cby.operation.common.ext.getId
import com.cby.operation.common.util.CbyLogUtils
import com.cby.operation.common.util.WebUtils

/**
 * @description: 公共通用的WebDialogFragment
 *
 * @author ChenBaoyang
 * @date 7/22/21 17:31
 */
abstract class BaseWebDialogFragment : BaseDialogFragment() {

    protected lateinit var webView: WebView

    override fun layoutName() = "common_web"

    override fun parseArgs() {

    }

    override fun initView(view: View?) {
        webView = view!!.findViewById(getId("webView"))
    }

    override fun setSystemUi() {
        var uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or  //全屏
                View.SYSTEM_UI_FLAG_FULLSCREEN or  //隐藏导航栏
                View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
        uiOptions = uiOptions or 0x00001000
        dialog.window!!.decorView.systemUiVisibility = uiOptions
    }

    override fun setContent() {
        WebUtils.setWebSettings(webView)
        webView.setBackgroundColor(0)
        webView.background.alpha = 0
        webView.webChromeClient = object : WebChromeClient() {
            override fun onReceivedTitle(view: WebView, title: String) {
                super.onReceivedTitle(view, title)
            }

            override fun onProgressChanged(view: WebView, newProgress: Int) {
                super.onProgressChanged(view, newProgress)
            }
        }

        webView.webViewClient = object : WebViewClient() {
            override fun onPageFinished(view: WebView, url: String) {
                super.onPageFinished(view, url)
                CbyLogUtils.flog("加载完成")
            }

            override fun onReceivedError(
                view: WebView, request: WebResourceRequest,
                error: WebResourceError
            ) {
                CbyLogUtils.flog("onReceivedError：" + (error?.toString() ?: "网页加载失败"))
            }

            override fun onReceivedSslError(
                view: WebView, handler: SslErrorHandler,
                error: SslError
            ) {
                handler.proceed()
            }
        }
        setWebContent()
    }

    protected abstract fun setWebContent()

    override fun initListener() {

    }

}